CONTENTS | INDEX | PREV | NEXT
scanf
fscanf
sscanf
NAME
scanf - scan formatted text from stdin
fscanf - scan formatted text from a specified file pointer
sscanf - scan formatted text from a string buffer
SYNOPSIS
#include <stdio.h>
int n = scanf(ctl, ...);
int n = fscanf(fp, ctl, ...);
int n = sscanf(str, ctl, ...);
const char *ctl;
FILE *fp;
char *str;
FUNCTION
*scanf() routines scan the specified input file or buffer for fields
matching those specified in the ctl field.
The ctl field contains % constructions that relate an argument
pointer to the scanned text. The ctl field may also contain other
characters which must match the scanned text exactly, except for a
white space character which matches one or more white space
characters the scanned text.
A % construction in the ctl string is formatted as follows:
%[*][nnn][h/l/L]<conversion-specifier>
(1) An optional * that tells scanf to skip the specified
convresion (i.e. you specify no argument for this conversion).
Skipped conversions are not included in the return count.
(2) An optional integer that specifies the maximum field width.
(3) An optional modifier h, l, or L:
h when used with d,i,n,o,u,x,X specifies the argument
is a pointer to a short int rather than an int
l when used with d,i,o,u,x,X specifies the argument is
a pointer to a long int rather than an int
L when used with e,E,f,F,G specifies the argument is
a pointer to a long double rather than a double
(4) A conversion specifier
c Convert the number of characters specified by the field
width (default 1) into an array. The expected argument
is a pointer to an array of characters.
d Convert a decimal (base 10) number. The expected argument
is a pointer to an int.
e/E/f/g
scans a floating point number. If the 'L' modifier is used
a long double pointer is expected (NOT IMPLEMENTED IN DICE
YET). Otherwise a double is expected.
i Convert a number to an integer. The format should be
equivalent to that expected by strtol with a base argument
of 0 (i.e. allows any base construction).
n This does not read any text, but stores the number of
characters *scanf() has read up to this point into the
integer argument.
o Convert a number to an octal integer
p Read a sequence of characters in the same format as
printf's %p specifier and store into a pointer. The
corresponding argument is passed as a void **
s Read a string of non-white space characters and copy
into the array specified by the argument. The argument
should be of type (char *) and have enough space to
handle the string plus a nul terminator
x Read a base 16 number (hex) and copy into the passed
integer. This is equivalent to calling strtol with
a base of 16.
% (i.e. %%) matches a single %
[/] Scan a scanset. Scan the input stream and place the
characters into a char * buffer until a character is read
that does not match the scanset.
If a scan set begins with ^ (as in [^abcd]) then all
characters are allowed EXCEPT those specified in the
scanset. If a scanset begins as []abcd] or [^]abcd] then
the ']' character is included in the scan set and the
set is terminated by the next ']' character.
NOTE
refer to the file_pointer manual page for general information
all pointers to floating point storage MUST be pointers to
doubles.
EXAMPLE
/*
* NOTE: unscanned arguments are NOT cleared and will print out
* as garbage.
*/
#include <stdio.h>
main(ac, av)
int ac;
char *av[];
{
short a[4];
int b[2];
char buf1[32];
char buf2[32];
int n;
if (ac != 2) {
puts("Expected string to format!");
exit(1);
}
n = sscanf(av[1], "%hd %ho %hi %*hn%d %X %10c%10c",
a + 0, a + 1, a + 2, b + 0, b + 1,
buf1, buf2
);
printf("n = %dn", n);
printf("a: %d %d %d %dn", a[0], a[1], a[2], a[3]);
printf("b: %d %dn", b[0], b[1]);
printf("buf1: %sn", buf1);
printf("buf2: %sn", buf2);
return(0);
}
1> t:x "123 23 01000 22 0xFF abcdefghijklm as ds sd "
n = 7
a: 123 19 512 12
b: 22 255
buf1: abcdefghij
buf2: klm as ds
1>
INPUTS
char *ctl; format control string
FILE *fp; (fscanf) file pointer, (read to obtain text to format)
char *str; (sscanf) string pointer, text to format
RESULTS
int n; # of conversions that occured, -1 if no conversions
could be done (usually means EOF). May return
less than the number requested and this value does
not reflect any %* forms.
SEE ALSO
sprintf, printf, fprintf